package com.jvxb.manage.livable.service.dbMonitor.impl;

import com.baomidou.mybatisplus.core.conditions.Condition;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.common.utils.DateUtil;
import com.jvxb.manage.livable.entity.DbMonitorData;
import com.jvxb.manage.livable.entity.DbMonitorIndex;
import com.jvxb.manage.livable.service.DbMonitorDataService;
import com.jvxb.manage.livable.service.dbMonitor.DbMonitorIndexService;
import com.jvxb.manage.livable.service.dbMonitor.DbMonitorService;
import com.jvxb.manage.livable.vo.MonitorIndexParam;
import com.jvxb.manage.livable.vo.MonitorParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author jvxb
 * @since 2022-02-15
 */
@Service
public class DbMonitorServiceImpl implements DbMonitorService {

    @Autowired
    DbMonitorIndexService dbMonitorIndexService;
    @Autowired
    DbMonitorDataService dbMonitorDataService;

    @Override
    public List getMetricIndex(MonitorIndexParam monitorIndexParam) {
        List<DbMonitorIndex> monitorIndices = dbMonitorIndexService.list(Condition.<DbMonitorIndex>create()
                .eq(DbMonitorIndex.MONI_OBJ_TYPE, monitorIndexParam.getMoniObjType())
                .eq(CommonUtil.notNullOrEmpty(monitorIndexParam.getIsCommon()), DbMonitorIndex.ISCOMMON, monitorIndexParam.getIsCommon())
                .like(CommonUtil.notNullOrEmpty(monitorIndexParam.getMetricName()), DbMonitorIndex.METRIC_NAME, monitorIndexParam.getMetricName())
                .orderByDesc(DbMonitorIndex.WEIGHT)
        );
        return monitorIndices;
    }

    @Override
    public String getMetricUnitByMetricName(String metricType, String metricName) {
        DbMonitorIndex dbMonitorIndex = dbMonitorIndexService.getOne(Condition.<DbMonitorIndex>create()
                .eq(DbMonitorIndex.MONI_OBJ_TYPE, metricType)
                .eq(DbMonitorIndex.METRIC_NAME, metricName).select(DbMonitorIndex.METRIC_UNIT));
        return dbMonitorIndex == null ? "" : dbMonitorIndex.getMetricUnit();
    }

    @Override
    public List getMetricIndexData(MonitorParam monitorParam) {
        //获取监控数据值
        List<DbMonitorData> dbMonitorDataList = dbMonitorDataService.list(Condition.<DbMonitorData>create()
                .ge(DbMonitorData.CREATE_TIME, monitorParam.getStartTime())
                .le(DbMonitorData.CREATE_TIME, monitorParam.getEndTime())
                .eq(DbMonitorData.MONI_OBJ_TYPE, monitorParam.getMoniObjType())
                .eq(DbMonitorData.METRIC_NAME, monitorParam.getMetricName())
                .in(DbMonitorData.TAG, monitorParam.getTags().split(","))
                .orderByAsc(DbMonitorData.CREATE_TIME)
        );
        //格式化
        List<Map> format = formatMonitorData(dbMonitorDataList);
        return format;
    }

    private List<Map> formatMonitorData(List<DbMonitorData> dbMonitorDataList) {
        if (CommonUtil.isNullOrEmpty(dbMonitorDataList)) {
            return null;
        }
        List<Map> formatList = new ArrayList<>();
        //根据相同时间，合并tag
        Map<String, Map> timeTagMap = new LinkedHashMap<>();
        for (DbMonitorData monitorData : dbMonitorDataList) {
            String timeKey = DateUtil.DateToString(monitorData.getCreateTime(), "yyyy/MM/dd HH:mm:ss");
            Map dataMap = timeTagMap.get(timeKey);
            if (dataMap == null) {
                dataMap = new HashMap();
            }
            dataMap.put(monitorData.getTag(), monitorData.getValue());
            timeTagMap.put(timeKey, dataMap);
        }
        //将相同时间的多个tag，合并为一个map
        for (Map.Entry<String, Map> timeTagMapEntry : timeTagMap.entrySet()) {
            Map timeMap = CommonUtil.getMap("TIME", timeTagMapEntry.getKey());
            timeMap.putAll(timeTagMapEntry.getValue());
            formatList.add(timeMap);
        }
        return formatList;
    }
}
